package myjava.xml;

import java.io.File;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class ValidateAgainstSchema {

   public static void main(String[] args) {
      try {
         // define the type of schema - we use W3C:
         String schemaLang = "http://www.w3.org/2001/XMLSchema";

         // get validation driver:
         SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

         // create schema by reading it from an XSD file:
         Schema schema = factory.newSchema(new StreamSource(new File("/home/nourdine/development/java_me/src/myjava/xml/configFile.xsd")));
         Validator validator = schema.newValidator();

         // at last perform validation:
         validator.validate(new StreamSource(new File("/home/nourdine/development/java_me/src/myjava/xml/config-sample.xml")));

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}
